package com.itheima.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.POJO.Member;
import com.itheima.POJO.MemberDies;
import com.itheima.POJO.Order;
import com.itheima.dao.memberMapper;
import com.itheima.dao.orderMapper;
import com.itheima.dao.setmealMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.dao.memberMapper;
import com.itheima.entity.PageResult;
import com.itheima.service.memberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = memberService.class)
@Transactional
public class memberServiceImpl implements memberService {

    @Autowired
    private memberMapper memberMapper;

    @Autowired
    private orderMapper orderMapper;
    @Autowired
    private setmealMapper setmealMapper;

    @Override
    public Member findMemberBYtelephone(String telephone) {
        Member member = memberMapper.findMemberBYtelephone(telephone);
        return member;
    }

    @Override
    public void addMember(Member member) {
        if (member.getPassword() != null) {
            String password = MD5Utils.md5(member.getPassword());
            member.setPassword(password);
        }
        memberMapper.addMember(member);
    }

    @Override
    public List<Integer> findMemberCountByMouth(List<String> arrayList) {
        List<Integer> counts = new ArrayList<>();
        for (String dataString : arrayList) {
            String replace = dataString.replace(".", "-");
            Integer count = memberMapper.findMemberCountByMouth(replace);
            counts.add(count);
        }

        return counts;
    }

    @Override
    public PageResult findPageMMM(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Member> memberPage = memberMapper.findByCondition(queryString);
        List<Member> result = memberPage.getResult();
        for (Member member : result) {
            Integer id = member.getId();
            List<String> keywordList = memberMapper.findAllhkeyword(id);
            member.setHealthprogramkeywordList(keywordList);
        }
        return new PageResult(memberPage.getTotal(), result);
    }

    @Override
    public Member findOne(Integer id) {
        Member member = memberMapper.findOneById(id);
        String sex = member.getSex();
        if ("o".equals(sex) || sex == null) {
            member.setSex("人妖");
        } else if ("1".equals(sex)) {
            member.setSex("男");
        } else if ("2".equals(sex)) {
            member.setSex("女");
        }
        MemberDies memberDies = memberMapper.findHistoryById(id);
        if (memberDies == null) {
            memberDies = new MemberDies();
            memberDies.setFamily("无");
            memberDies.setMedical("无");
            memberDies.setPrevious("无");
        }
        member.setMemberDies(memberDies);
        return member;
    }

    @Override
    public Map<String, Object> findMedicalInformationById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        String fileNumber = memberMapper.findFileNumberById(id);
        Order order = orderMapper.findOrderByMemberId(id);
        if (order == null) {
            map.put("fileNumber", fileNumber);
            map.put("orderDate", "无");
            map.put("setMealName", "无");
            map.put("orderStatus", "无");
            return map;
        }
        String setMealName = setmealMapper.findNameById(order.getSetmealId());
        map.put("fileNumber", fileNumber);
        map.put("orderDate", order.getOrderDate());
        map.put("setMealName", setMealName);
        map.put("orderStatus", order.getOrderStatus());
        return map;
    }

    @Override
    public List<Integer> findHealthProgramIdsById(Integer id) {

        List<Integer> list = memberMapper.findHealthProgramIdsByMemberId(id);
        return list;
    }

    @Override
    public void healthEdit(Map map, Integer[] healthProgramIds) {
        Member member = new Member();
        member.setHealthManager((String) map.get("value"));
        member.setFileNumber((String) map.get("fileNumber"));
        memberMapper.healthEdit(member);
        int memberId = memberMapper.findIdByFileNumber((String) map.get("fileNumber"));
        List<Integer> list = memberMapper.findHealthByMemberId(memberId);

        if (list.size() > 0 && list != null) {
            memberMapper.deleteHealthProgramIdByMemberId(memberId);
        }
        setMemberIdAndHealthProgramId(memberId, healthProgramIds);
    }

    @Override
    public void deleteById(Integer id) {
        memberMapper.deleteHealthProgramIdByMemberId(id);
        memberMapper.deleteDiesById(id);
        memberMapper.deleteOrderById(id);
        memberMapper.deleteMemberInfoById(id);
        memberMapper.deleteMemberById(id);
    }

    public void setMemberIdAndHealthProgramId(Integer memberId, Integer[] healthProgramIds) {
        if (healthProgramIds != null && healthProgramIds.length > 0) {
            for (Integer healthProgramId : healthProgramIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("memberId", memberId);
                map.put("healthProgramId", healthProgramId);
                memberMapper.setMemberIdAndHealthProgramId(map);
            }
        }
    }
    public void add(com.itheima.VO.Member member) {
        memberMapper.addAllMember(member);
        memberMapper.addMemberDies(member);
        memberMapper.addMemberInfo(member);
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<com.itheima.VO.Member> page = memberMapper.findMember(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public com.itheima.VO.Member findMemberById(Integer id) {
        com.itheima.VO.Member member = memberMapper.findMemberById(id);

        return member;
    }

    @Override
    public void editMember(com.itheima.VO.Member member) {
        memberMapper.editMember(member);
        memberMapper.editMemberInfo(member);
        memberMapper.editMemberDies(member);
    }

    @Override
    public void delete(Integer id) {
        memberMapper.deleteMemberInfo(id);
        memberMapper.deleteMemberDies(id);
        memberMapper.deleteMember(id);
    }
}
